package org.ceteca.explica.client.partners;

import java.util.HashSet;

import org.ceteca.explica.client.gui.common.CommonClient;
import org.ceteca.explica.core.common.BaseVO;
import org.ceteca.explica.core.common.MultiSelectionSubentityOT;
import org.ceteca.explica.core.common.SelectionSubentityOT;
import org.ceteca.explica.core.partners.ContactOT;
import org.ceteca.explica.core.partners.InterfacePartners;
import org.ceteca.explica.core.partners.RappelIntervalOT;
import org.ceteca.explica.core.partners.RappelTypeOT;
import org.ceteca.explica.core.util.Converter;
import org.ceteca.explica.core.util.OperationResult;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;


/**
 * @author Juan C. Díaz (juancarlosdiaz@ceteca.net) <br>
 *         Copyright (c) 2009 Fundación Centro Tecnolóxico da Carne
 * 
 * <br>
 * <br>
 *         This program is free software: you can redistribute it and/or modify
 *         it under the terms of the GNU General Public License as published by
 *         the Free Software Foundation, either version 3 of the License, or (at
 *         your option) any later version. <br/>
 *         This program is distributed in the hope that it will be useful, but
 *         WITHOUT ANY WARRANTY 
 *         <br>without even the implied warranty of
 *         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *         General Public License for more details. <br/>
 *         You should have received a copy of the GNU General Public License
 *         along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
public class RappelIntervalClient extends CommonClient {

	private Text entryRappelIntervalCode, entryRappelIntervalLowerLimit,
			entryRappelIntervalUpperLimit, entryRappelIntervalDisccountRate,
			entryRappelIntervalRappelType, entryVersion;
	private Button buttonRappelTypeRappelInterval;
	

	public RappelIntervalClient() {
		super(RappelIntervalOT.class, "", "");
		setEditGladePath(InterfacePartners.EXPLICA_EDIT_RAPPELINTERVAL_GLADE_PATH);
		setDialogName(InterfacePartners.EDIT_RAPPELINTERVAL_DIALOG_NAME);
		//this.when_delete_only_clear_parent_field=true;

	}

	
	

	
	@Override
	public void clearEntityData() {
		RappelIntervalOT ri = new RappelIntervalOT();
		if (isDetail()) {
			if (this.entityDetailView.getSubentityData().getParentEntity()
					.getEntity().getKey().equals(
							InterfacePartners.EXPLICA_ENTITY_RAPPELTYPE_ID)) {
				RappelTypeOT parentPriceList = (RappelTypeOT) entityDetailView
						.getSubentityData().getParentEntity()
						.getSelectedEntity();
				ri.setRappelType(parentPriceList);
				entryRappelIntervalRappelType.setEnabled(false);
				buttonRappelTypeRappelInterval.setEnabled(false);

			}
		} else {
			entryRappelIntervalRappelType.setText("");
			entryRappelIntervalRappelType.setEnabled(false);
			buttonRappelTypeRappelInterval.setEnabled(false);
		}
		setWidgetsValues(ri);

	}

	@Override
	public BaseVO getEntityData() {

		RappelIntervalOT entity = new RappelIntervalOT();

		if (this.entEditView != null) {
			this.getWidgets();
			entity.setLowerLimit(Converter.getInstance().parseBigDecimal(
					entryRappelIntervalLowerLimit.getText()));
			entity.setUpperLimit(Converter.getInstance().parseBigDecimal(
					entryRappelIntervalUpperLimit.getText()));
			entity.setDisccountRate(Converter.getInstance().parseFloat(
					entryRappelIntervalDisccountRate.getText()));
			entity.setRappelType((RappelTypeOT) (entryRappelIntervalRappelType
					.getData()));
			
			if (selectedEntity != null) {
				entity.setVersion(((RappelIntervalOT) this.selectedEntity)
						.getVersion());
				entity
						.setIdRappelInterval(((RappelIntervalOT) this.selectedEntity)
								.getIdRappelInterval());
			}
		}

		return entity;
	}




	@Override
	public OperationResult validateEntity() {
		return null;
	}

	@Override
	public void setEditionMode(boolean enabled) {
		getWidgets();
		entryRappelIntervalCode.setEnabled(false);
		entryRappelIntervalDisccountRate.setEnabled(enabled);
		entryRappelIntervalLowerLimit.setEnabled(enabled);
		// entryRappelIntervalRappelType.setEnabled(enabled);
		entryRappelIntervalUpperLimit.setEnabled(enabled);
		// buttonRappelTypeRappelInterval.setEnabled(enabled);
		entryVersion.setEnabled(false);
		if (isDetail()) {
			if (this.entityDetailView.getSubentityData().getParentEntity()
					.getEntity().getKey().equals(
							InterfacePartners.EXPLICA_ENTITY_RAPPELTYPE_ID)) {
				entryRappelIntervalRappelType.setEnabled(false);
				buttonRappelTypeRappelInterval.setEnabled(false);
			}
		}
	}

	public void getWidgets() {

		boolean allOk = true;
		if (this.entEditView == null) {
			this.logger.error("Fatal error: Cant not get JGlade");
			System.exit(-1);
		}

		if ((entryRappelIntervalCode = ((Text) this.getWidget(
				InterfacePartners.EDIT_RAPPELINTERVAL_CODE_WIDGETNAME))) == null) {
			allOk = false;
			this.logger.error("Error getting entry Rappel Interval Widget:"
					+ InterfacePartners.EDIT_RAPPELINTERVAL_CODE_WIDGETNAME);

		}

		if ((entryRappelIntervalDisccountRate = ((Text) this
				.getWidget(
						InterfacePartners.EDIT_RAPPELINTERVAL_DISCCOUNTRATE_WIDGETNAME))) == null) {
			allOk = false;
			this.logger.error("Error getting entry Rappel Interval Widget:"
					+ InterfacePartners.EDIT_RAPPELINTERVAL_CODE_WIDGETNAME);

		}

		if ((entryRappelIntervalLowerLimit = ((Text) this
				.getWidget(
						InterfacePartners.EDIT_RAPPELINTERVAL_LOWERLIMIT_WIDGETNAME))) == null) {
			allOk = false;
			this.logger
					.error("Error getting entry Rappel Interval Widget:"
							+ InterfacePartners.EDIT_RAPPELINTERVAL_LOWERLIMIT_WIDGETNAME);

		}

		if ((entryRappelIntervalUpperLimit = ((Text) this
				.getWidget(
						InterfacePartners.EDIT_RAPPELINTERVAL_UPPERLIMIT_WIDGETNAME))) == null) {
			allOk = false;
			this.logger
					.error("Error getting entry Rappel Interval Widget:"
							+ InterfacePartners.EDIT_RAPPELINTERVAL_UPPERLIMIT_WIDGETNAME);

		}
		if ((entryVersion = ((Text) this
				.getWidget(
						InterfacePartners.EDIT_VERSION_WIDGETNAME))) == null) {
			allOk = false;
			this.logger
					.error("Error getting entry Version Widget:"
							+ InterfacePartners.EDIT_VERSION_WIDGETNAME);

		}
		if ((entryRappelIntervalRappelType = ((Text) this
				.getWidget(
						InterfacePartners.EDIT_RAPPELINTERVAL_RAPPELTYPE_WIDGETNAME))) == null) {
			allOk = false;
			this.logger
					.error("Error getting entry Rappel Interval Widget:"
							+ InterfacePartners.EDIT_RAPPELINTERVAL_RAPPELTYPE_WIDGETNAME);

		}
		
		if ((entryVersion = ((Text) this
				.getWidget(
						InterfacePartners.EDIT_VERSION_WIDGETNAME))) == null) {
			allOk = false;
			this.logger
					.error("Error getting entry Rappel Interval Widget:"
							+ InterfacePartners.EDIT_VERSION_WIDGETNAME);

		}

		if ((buttonRappelTypeRappelInterval = ((Button) this
				.getWidget(
						InterfacePartners.EDIT_RAPPELINTERVAL_SELECT_RAPPELTYPE_BUTTON_WIDGETNAME))) == null) {
			allOk = false;
			this.logger
					.error("Error getting entry Rappel Interval Widget:"
							+ InterfacePartners.EDIT_RAPPELINTERVAL_SELECT_RAPPELTYPE_BUTTON_WIDGETNAME);

		}

		if (!allOk)
			System.exit(-1);

	}

	public void setWidgetsValues(BaseVO entity) {

		RappelIntervalOT ent = (RappelIntervalOT) entity;
		getWidgets();
		entryRappelIntervalCode.setText(Converter.getInstance().toString(
				ent.getIdRappelInterval()));
		entryRappelIntervalDisccountRate.setText(Converter.getInstance()
				.toString(ent.getDisccountRate()));
		entryRappelIntervalLowerLimit.setText(Converter.getInstance().toString(
				ent.getLowerLimit()));
		entryRappelIntervalUpperLimit.setText(Converter.getInstance().toString(
				ent.getUpperLimit()));
		entryVersion.setText(Converter.getInstance().toString(((RappelIntervalOT)entity).getVersion()));
		
		
		
		
		if (ent.getRappelType() != null) {
			entryRappelIntervalRappelType.setText(ent.getRappelType()
					.getDescription());
			entryRappelIntervalRappelType.setData(ent.getRappelType());
		}

	}

	@Override
	public void buildSelectionSets() {
		lSelectionSubentities = new HashSet<SelectionSubentityOT>();
		lSelectionSubentities.add(new SelectionSubentityOT(
				InterfacePartners.EXPLICA_ENTITY_RAPPELTYPE_ID,
				InterfacePartners.EDIT_RAPPELINTERVAL_RAPPELTYPE_WIDGETNAME,
				this, buttonRappelTypeRappelInterval));

		lMultiSelectionSubentities = new HashSet<MultiSelectionSubentityOT>();

	}

	@Override
	public void init_specific_components() {
		// TODO Auto-generated method stub
		
	}




	@Override
	public void build_extension_fields_view() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void fillEntityData() {
		if (this.selectedEntity != null && this.entEditView != null) {
			getWidgets();
			setWidgetsValues((RappelIntervalOT) selectedEntity);
		}
	}
}
